const STORAGE_KEY = 'ollama_server';

window.onload = () => {
    const input = document.getElementById('ollamaInput');
    const saveBtn = document.getElementById('saveBtn');
    const closeSTBtn = document.getElementById('closeSTBtn');
    const msg = document.getElementById('msg');

    // 页面加载时读取 localStorage
    const savedValue = localStorage.getItem(STORAGE_KEY);
    if (savedValue) {
        input.value = savedValue;
    }

    // 点击保存按钮时写入 localStorage
    saveBtn.addEventListener('click', () => {
        const val = input.value.trim();
        if (!val) {
            msg.style.color = 'red';
            msg.textContent = '请输入有效的地址';
            return;
        }
        localStorage.setItem(STORAGE_KEY, val);
        msg.style.color = 'green';
        msg.textContent = '保存成功！';
        setTimeout(() => { msg.textContent = ''; }, 2000);
    });

    // 关闭 iframe（悬浮框）
    closeSTBtn.addEventListener('click', () => {
        // 告诉父页面关闭 iframe
        window.parent.postMessage('close_setting_frame', '*');
    });
};